package com.zx.map.controller;

import com.zx.map.service.SettleCardInfoService;
import com.zx.map.service.TodoTaskService;
import com.zx.map.service.domain.TodoTaskDomain;
import com.zx.map.service.model.*;
import com.zx.map.utils.MyBeanUtils;
import com.zx.platform.utils.AccountObjectSerializerUtil;
import com.zx.platform.utils.PmpUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/todoTask")
public class TodoTaskController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(TodoTaskController.class);

	@Autowired
	private TodoTaskService todoTaskService;
	@Autowired
	private SettleCardInfoService settleCardInfoService;

	/**
	 * 进入列表页面
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/list")
	public String list(TodoTaskDomain domain, Model model,
			HttpServletRequest request) {

		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}

			Map map = todoTaskService
					.getPageByDomain(pageSize, pageNum, domain);
			List<TodoTaskModel> todoTaskModelList = (List<TodoTaskModel>) map
					.get("list");
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", todoTaskModelList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/platform/todoTask/todoTaskList";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 表单提交日期绑定
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	/**
	 * 弹出的新增修改页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(TodoTaskModel todoTaskModel, Model model,
			HttpServletRequest request) {
		try {

			Integer orderId = todoTaskModel.getTaskId();
			if (orderId != -1 && orderId != null) {
				todoTaskModel = todoTaskService.getModelByKey(orderId);
			}
			model.addAttribute("model", todoTaskModel);
			return "/jsp/platform/todoTask/todoTaskEdit";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * @return
	 */
	@RequestMapping("/update")
	public @ResponseBody
	TodoTaskModel update(TodoTaskModel model) {
		try {
			todoTaskService.update(model);
			model.setSuccess(true);
			model.setMsg("保存信息成功");
			log.debug("保存信息成功");
		} catch (Exception e) {
			model.setSuccess(false);
			model.setMsg("保存信息失败");
			log.error("保存信息失败" + e.getMessage(), e);
		}
		return model;
	}

	/**
	 * @return
	 */
	@RequestMapping("/delete")
	public @ResponseBody
	TodoTaskModel delete(TodoTaskModel model) {
		try {
			todoTaskService.delete(model);
			model.setSuccess(true);
			model.setMsg("删除信息成功");
			log.debug("删除信息成功");
		} catch (Exception e) {
			model.setSuccess(false);
			model.setMsg("删除信息失败");
			log.error("删除信息失败" + e.getMessage(), e);
		}
		return model;
	}

	/**
	 * @return
	 */
	@RequestMapping("/add")
	public @ResponseBody
	TodoTaskModel add(TodoTaskModel model) {
		try {
			todoTaskService.add(model);
			model.setSuccess(true);
			model.setMsg("新增信息成功");
			log.debug("新增信息成功");
		} catch (Exception e) {
			model.setSuccess(false);
			model.setMsg("新增信息失败");
			log.error("新增信息失败" + e.getMessage(), e);
		}
		return model;
	}
	/**
	 * 结算银行更换
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/settleCardInfoChangeAdd")
	public @ResponseBody
	TodoTaskModel settleCardInfoChangeAdd(SettleCardInfoModel settleCardInfoModel) {
		TodoTaskModel todoTaskModel = new TodoTaskModel();
		try {
			//设置任务属性
			todoTaskModel.setTaskType("6");
			todoTaskModel.setApplyOperator("2");
			todoTaskService.addTodoTask(settleCardInfoModel, todoTaskModel);
			todoTaskModel.setSuccess(true);
			todoTaskModel.setMsg("申请成功");
			log.debug("申请成功");
		} catch (Exception e) {
			todoTaskModel.setSuccess(false);
			todoTaskModel.setMsg("申请失败");
			log.error("申请失败" + e.getMessage(), e);
		}
		return todoTaskModel;
	}
	/**
	 * 银行更换复核页面 edit by wenjw 2015.5.14
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/settleCardInfoApproveTask")
	public String settleCardInfoApproveTask(TodoTaskDomain domain, Model model,
			HttpServletRequest request) {

		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}

			//任务类型是结算银行卡更换  6
			domain.setTaskType("6");
			Map map = todoTaskService
					.getPageByDomain(pageSize, pageNum, domain);
			List<TodoTaskModel> todoTaskModelList = (List<TodoTaskModel>) map
					.get("list");
			//转换
			List<SettleCardInfoTodoTask> modelList = new ArrayList();
			for(TodoTaskModel todoTaskModel:todoTaskModelList){
				SettleCardInfoTodoTask settleCardInfoTodoTask = new SettleCardInfoTodoTask();
				settleCardInfoTodoTask.setTodoTaskModel(todoTaskModel);
				SettleCardInfoModel settleCardInfoModel =MyBeanUtils.jsonToBean(todoTaskModel.getTaskInfo(),SettleCardInfoModel.class);
				settleCardInfoTodoTask.setSettleCardInfoModel(settleCardInfoModel);
				modelList.add(settleCardInfoTodoTask);
			}
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", modelList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/map/todoTask/settleCardInfoApproveTask";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}
	
	/**
	 * 调账申请页面 edit by wenjw 2015.5.14
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/adjustApplyTask")
	public String adjustApplyTask(TodoTaskDomain domain, Model model,
			HttpServletRequest request) {

		try {
			return "/jsp/map/todoTask/adjustApplyTask";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 调账申请确认提交 edit by wenjw 2015.5.14
	 *
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/adjustApplyTaskAdd")
	public @ResponseBody
	TodoTaskModel adjustApplyTaskAdd(AdjustLogTodoTaskModel adjustLogTodoTaskModel) {
		TodoTaskModel todoTaskModel = new TodoTaskModel();
		try {
			
			//相关属性已经在页面传过来了
			
			todoTaskService.addTodoTask(adjustLogTodoTaskModel.getAdjustLogModel(), adjustLogTodoTaskModel.getTodoTaskModel());
			todoTaskModel.setSuccess(true);
			todoTaskModel.setMsg("申请成功");
			log.debug("申请成功");
		} catch (Exception e) {
			todoTaskModel.setSuccess(false);
			todoTaskModel.setMsg("申请失败");
			log.error("申请失败" + e.getMessage(), e);
		}
		return todoTaskModel;
	}

	/**
	 * 调账复核页面 edit by wenjw 2015.5.14
	 *
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/adjustApproveTask")
	public String adjustApproveTask(TodoTaskDomain domain, Model model,
			HttpServletRequest request) {

		try {// 初始化信息
			Integer pageNum = domain.getPageNum();
			Integer pageSize = domain.getPageSize();
			Integer pageCount = domain.getPageCount();
			if (null == pageNum) {
				pageNum = 1;
			}
			if (null == pageSize) {
				pageSize = 10;
			}

			//任务类型是调账的 7
			domain.setTaskType("7");
			Map map = todoTaskService
					.getPageByDomain(pageSize, pageNum, domain);
			List<TodoTaskModel> todoTaskModelList = (List<TodoTaskModel>) map
					.get("list");
			//转换
			List<AdjustLogTodoTaskModel> modelList = new ArrayList();
			for(TodoTaskModel todoTaskModel:todoTaskModelList){
				AdjustLogTodoTaskModel adjustLogTodoTask = new AdjustLogTodoTaskModel();
				adjustLogTodoTask.setTodoTaskModel(todoTaskModel);
				AdjustLogModel adjustLogModel = MyBeanUtils.jsonToBean(todoTaskModel.getTaskInfo(),AdjustLogModel.class);
				adjustLogTodoTask.setAdjustLogModel(adjustLogModel);
				modelList.add(adjustLogTodoTask);
			}
			int recordCount = Integer.valueOf(map.get("count").toString());
			// 页面信息
			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", modelList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", domain);
			return "/jsp/map/todoTask/adjustApproveTask";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
     * 调账复核意见页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/approveTaskAuditEdit")
    public String approveTaskAuditEdit(TodoTaskModel todoTaskModel, Model model, HttpServletRequest request) {
    	try{

	    	model.addAttribute("todoTaskModel", todoTaskModel);
	        return "/jsp/map/todoTask/approveTaskAuditEdit";
	    } catch (Exception e) {
	        log.error("内部异常", e);
	        model.addAttribute("errorMsg", "出错了");
	        return "/jsp/platform/error";
	    }
    }
	/**
	 * 统一的审核功能
	 * edit by wenjw 2015.5.14
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/approveTaskAudit")
	public @ResponseBody
	TodoTaskModel approveTaskAudit(TodoTaskModel model) {
		try {
			todoTaskService.approveTaskAudit(model);
			model.setSuccess(true);
			String approveStatus = model.getApproveStatus();
			if("1".equals(approveStatus)){
				model.setMsg("审核通过成功");
			}else if("2".equals(approveStatus)){
				model.setMsg("审核拒绝成功");
			}
			log.debug("审核成功");
		} catch (Exception e) {
			e.printStackTrace();
			model.setSuccess(false);
			model.setMsg("审核失败");
			log.error("审核失败" + e.getMessage(), e);
		}
		return model;
	}
	/**
     * 结算银行更换修改申请
     * @param 
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/settleCardChangeApplyTask")
    public String settleCardChangeApplyTask(SettleCardInfoModel settleCardInfoModel, Model model, HttpServletRequest request) {
		try {
			Integer settleId = settleCardInfoModel.getSettleId();
			settleCardInfoModel = settleCardInfoService.getModelByKey(settleId);
			model.addAttribute("model", settleCardInfoModel);
			return "/jsp/map/todoTask/settleCardChangeApplyTask";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}
    /**
	 * 银行更换申请确认提交 edit by wenjw 2015.5.14
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/settleCardChangeApplyTaskAdd")
	public @ResponseBody
	TodoTaskModel settleCardChangeApplyTaskAdd(SettleCardChangeTodoTask settleCardChangeTodoTask) {
		TodoTaskModel todoTaskModel = new TodoTaskModel();
		try {
			todoTaskModel = settleCardChangeTodoTask.getTodoTaskModel();
			todoTaskService.addTodoTask(settleCardChangeTodoTask.getSettleCardInfoModel(),todoTaskModel);
			todoTaskModel.setSuccess(true);
			todoTaskModel.setMsg("申请成功");
			log.debug("申请成功");
		} catch (Exception e) {
			todoTaskModel.setSuccess(false);
			todoTaskModel.setMsg("申请失败");
			log.error("申请失败" + e.getMessage(), e);
		}
		return todoTaskModel;
	}
	
	
    /**
	 * 终端绑定只申请
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/terminalBandingTaskAdd")
	public @ResponseBody
	TodoTaskModel terminalBandingTaskAdd(TerminalInfoTodoTask terminalInfoTodoTask) {
		TodoTaskModel todoTaskModel = new TodoTaskModel();
		try {
			todoTaskModel = terminalInfoTodoTask.getTodoTaskModel();
			todoTaskService.addTodoTask(terminalInfoTodoTask.getTerminalInfoModel(),todoTaskModel);
			todoTaskModel.setSuccess(true);
			todoTaskModel.setMsg("申请成功");
			log.debug("申请成功");
		} catch (Exception e) {
			todoTaskModel.setSuccess(false);
			todoTaskModel.setMsg("申请失败");
			log.error("申请失败" + e.getMessage(), e);
		}
		return todoTaskModel;
	}
	
	/**
	 * 终端绑定只申请
	 *
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/terminalUnBandingTaskAdd")
	public @ResponseBody
	TodoTaskModel terminalUnBandingTaskAdd(TerminalInfoTodoTask terminalInfoTodoTask) {
		TodoTaskModel todoTaskModel = new TodoTaskModel();
		try {
			todoTaskModel = terminalInfoTodoTask.getTodoTaskModel();
			todoTaskService.addTodoTask(terminalInfoTodoTask.getTerminalInfoModel(),todoTaskModel);
			todoTaskModel.setSuccess(true);
			todoTaskModel.setMsg("解绑申请已受理，请等待审核!");
			log.debug("解绑申请已受理，请等待审核!");
		} catch (Exception e) {
			todoTaskModel.setSuccess(false);
			todoTaskModel.setMsg("解绑申请提交失败");
			log.error("解绑申请提交失败" + e.getMessage(), e);
		}
		return todoTaskModel;
	}

	@RequestMapping("/modOldData")
	@ResponseBody
	public String list(){
		List<TodoTaskModel> list =  todoTaskService.getAllList();
		try {
			for(TodoTaskModel t :list){

				Object object= AccountObjectSerializerUtil.unserialize2(t.getTaskInfo());
				String taskInfo = MyBeanUtils.beanToJson(object);
				t.setTaskInfo(taskInfo);
				todoTaskService.update(t);
			}
		}catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return "/jsp/platform/todoTask/todoTaskList";

	}

}
